package com.zzyl.service.impl;


import cn.hutool.core.bean.BeanUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zzyl.base.PageResponse;
import com.zzyl.dto.NursingLevelAddDto;
import com.zzyl.dto.NursingLevelDto;
import com.zzyl.entity.NursingLevel;
import com.zzyl.entity.NursingPlan;
import com.zzyl.mapper.HulidengjiMapper;
import com.zzyl.service.HulidengjiService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;


@Service
public class HulidengjiImpl  implements HulidengjiService {
    @Autowired
    private HulidengjiMapper hulidengjiMapper;

    @Override
    public PageResponse<NursingLevel> listAll(NursingLevelDto dto) {
        PageHelper.startPage(dto.getPageNum(), dto.getPageSize());
        Page<NursingLevel> page = hulidengjiMapper.fneye(dto);
        return PageResponse.of(page, NursingLevel.class);
    }

//    @Override
//    public String insert(NursingLevelAddDto dto) {
//
//        NursingLevel nursingLevel =
//        return hulidengjiMapper.insert(nursingLevel);
//    }

    @Override
    public void insert(NursingLevelAddDto dto) {
        NursingLevel nursingLevel = BeanUtil.copyProperties(dto, NursingLevel.class);
        hulidengjiMapper.insert(nursingLevel);
    }

    @Override
    public PageResponse<NursingLevel> search(NursingLevelDto dto) {
        // 根据传入的dto中的条件进行查询
        List<NursingLevel> nursingPlans = hulidengjiMapper.search(dto);

        // 对查询结果进行分页处理
        int totalCount = nursingPlans.size();
        int pageSize = dto.getPageSize();
        int totalPages = (int) Math.ceil((double) totalCount / pageSize);

        int startIndex = (dto.getPageNum() - 1) * pageSize;
        int endIndex = Math.min(startIndex + pageSize, totalCount);

        List<NursingLevel> pagedNursingPlans = nursingPlans.subList(startIndex, endIndex);

        // 构建分页响应对象
        PageResponse<NursingLevel> pageResponse = new PageResponse<>();
        pageResponse.setTotal((long) totalCount);
        pageResponse.setPageSize(pageSize);
        pageResponse.setPages((long) totalPages);
        pageResponse.setPage(dto.getPageNum());
        pageResponse.setRecords(pagedNursingPlans);

        return pageResponse;
    }

    @Override
    public NursingLevel getById(Long id) {

        return hulidengjiMapper.getById(id);
    }


//        修改
    @Override
    public void update(NursingLevelAddDto dto) {
        NursingLevel nl = BeanUtil.copyProperties(dto, NursingLevel.class);
        hulidengjiMapper.update(nl);
    }

    @Override
    public void delete(Long id) {

        hulidengjiMapper.delete(id);

    }

    @Override
    public void updateStatus(Long id, Integer status) {
        NursingLevel nursingLevel = new NursingLevel();
        nursingLevel.setId(id);
        nursingLevel.setStatus(status);
        hulidengjiMapper.update(nursingLevel);

    }




}

